home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / bin / xdg-open < prev    next >
Encoding:
Text File  |  2010-09-15  |  11.8 KB  |  502 lines

  1. #!/bin/sh
  2. #---------------------------------------------
  3. #   xdg-open
  4. #
  5. #   Utility script to open a URL in the registered default application.
  6. #
  7. #   Refer to the usage() function below for usage.
  8. #
  9. #   Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
  10. #   Copyright 2006, Jeremy White <jwhite@codeweavers.com>
  11. #
  12. #   LICENSE:
  13. #
  14. #   Permission is hereby granted, free of charge, to any person obtaining a
  15. #   copy of this software and associated documentation files (the "Software"),
  16. #   to deal in the Software without restriction, including without limitation
  17. #   the rights to use, copy, modify, merge, publish, distribute, sublicense,
  18. #   and/or sell copies of the Software, and to permit persons to whom the
  19. #   Software is furnished to do so, subject to the following conditions:
  20. #
  21. #   The above copyright notice and this permission notice shall be included
  22. #   in all copies or substantial portions of the Software.
  23. #
  24. #   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  25. #   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. #   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  27. #   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  28. #   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  29. #   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  30. #   OTHER DEALINGS IN THE SOFTWARE.
  31. #
  32. #---------------------------------------------
  33.  
  34. manualpage()
  35. {
  36. cat << _MANUALPAGE
  37. Name
  38.  
  39. xdg-open - opens a file or URL in the user's preferred application
  40.  
  41. Synopsis
  42.  
  43. xdg-open { file | URL }
  44.  
  45. xdg-open { --help | --manual | --version }
  46.  
  47. Description
  48.  
  49. xdg-open opens a file or URL in the user's preferred application. If a URL is
  50. provided the URL will be opened in the user's preferred web browser. If a file
  51. is provided the file will be opened in the preferred application for files of
  52. that type. xdg-open supports file, ftp, http and https URLs.
  53.  
  54. xdg-open is for use inside a desktop session only. It is not recommended to use
  55. xdg-open as root.
  56.  
  57. Options
  58.  
  59. --help
  60.     Show command synopsis.
  61. --manual
  62.     Show this manualpage.
  63. --version
  64.     Show the xdg-utils version information.
  65.  
  66. Exit Codes
  67.  
  68. An exit code of 0 indicates success while a non-zero exit code indicates
  69. failure. The following failure codes can be returned:
  70.  
  71. 1
  72.     Error in command line syntax.
  73. 2
  74.     One of the files passed on the command line did not exist.
  75. 3
  76.     A required tool could not be found.
  77. 4
  78.     The action failed.
  79.  
  80. Examples
  81.  
  82. xdg-open 'http://www.freedesktop.org/'
  83.  
  84. Opens the Freedesktop.org website in the user's default browser
  85.  
  86. xdg-open /tmp/foobar.png
  87.  
  88. Opens the PNG image file /tmp/foobar.png in the user's default image viewing
  89. application.
  90.  
  91. _MANUALPAGE
  92. }
  93.  
  94. usage()
  95. {
  96. cat << _USAGE
  97. xdg-open - opens a file or URL in the user's preferred application
  98.  
  99. Synopsis
  100.  
  101. xdg-open { file | URL }
  102.  
  103. xdg-open { --help | --manual | --version }
  104.  
  105. _USAGE
  106. }
  107.  
  108. #@xdg-utils-common@
  109.  
  110. #----------------------------------------------------------------------------
  111. #   Common utility functions included in all XDG wrapper scripts
  112. #----------------------------------------------------------------------------
  113.  
  114. DEBUG()
  115. {
  116.   [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && return 0;
  117.   [ ${XDG_UTILS_DEBUG_LEVEL} -lt $1 ] && return 0;
  118.   shift
  119.   echo "$@" >&2
  120. }
  121.  
  122. #-------------------------------------------------------------
  123. # Exit script on successfully completing the desired operation
  124.  
  125. exit_success()
  126. {
  127.     if [ $# -gt 0 ]; then
  128.         echo "$@"
  129.         echo
  130.     fi
  131.  
  132.     exit 0
  133. }
  134.  
  135.  
  136. #-----------------------------------------
  137. # Exit script on malformed arguments, not enough arguments
  138. # or missing required option.
  139. # prints usage information
  140.  
  141. exit_failure_syntax()
  142. {
  143.     if [ $# -gt 0 ]; then
  144.         echo "xdg-open: $@" >&2
  145.         echo "Try 'xdg-open --help' for more information." >&2
  146.     else
  147.         usage
  148.         echo "Use 'man xdg-open' or 'xdg-open --manual' for additional info."
  149.     fi
  150.  
  151.     exit 1
  152. }
  153.  
  154. #-------------------------------------------------------------
  155. # Exit script on missing file specified on command line
  156.  
  157. exit_failure_file_missing()
  158. {
  159.     if [ $# -gt 0 ]; then
  160.         echo "xdg-open: $@" >&2
  161.     fi
  162.  
  163.     exit 2
  164. }
  165.  
  166. #-------------------------------------------------------------
  167. # Exit script on failure to locate necessary tool applications
  168.  
  169. exit_failure_operation_impossible()
  170. {
  171.     if [ $# -gt 0 ]; then
  172.         echo "xdg-open: $@" >&2
  173.     fi
  174.  
  175.     exit 3
  176. }
  177.  
  178. #-------------------------------------------------------------
  179. # Exit script on failure returned by a tool application
  180.  
  181. exit_failure_operation_failed()
  182. {
  183.     if [ $# -gt 0 ]; then
  184.         echo "xdg-open: $@" >&2
  185.     fi
  186.  
  187.     exit 4
  188. }
  189.  
  190. #------------------------------------------------------------
  191. # Exit script on insufficient permission to read a specified file
  192.  
  193. exit_failure_file_permission_read()
  194. {
  195.     if [ $# -gt 0 ]; then
  196.         echo "xdg-open: $@" >&2
  197.     fi
  198.  
  199.     exit 5
  200. }
  201.  
  202. #------------------------------------------------------------
  203. # Exit script on insufficient permission to write a specified file
  204.  
  205. exit_failure_file_permission_write()
  206. {
  207.     if [ $# -gt 0 ]; then
  208.         echo "xdg-open: $@" >&2
  209.     fi
  210.  
  211.     exit 6
  212. }
  213.  
  214. check_input_file()
  215. {
  216.     if [ ! -e "$1" ]; then
  217.         exit_failure_file_missing "file '$1' does not exist"
  218.     fi
  219.     if [ ! -r "$1" ]; then
  220.         exit_failure_file_permission_read "no permission to read file '$1'"
  221.     fi
  222. }
  223.  
  224. check_vendor_prefix()
  225. {
  226.     file_label="$2"
  227.     [ -n "$file_label" ] || file_label="filename"
  228.     file=`basename "$1"`
  229.     case "$file" in
  230.        [a-zA-Z]*-*)
  231.          return
  232.          ;;
  233.     esac
  234.  
  235.     echo "xdg-open: $file_label '$file' does not have a proper vendor prefix" >&2
  236.     echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated' >&2
  237.     echo 'with a dash ("-"). An example '"$file_label"' is '"'example-$file'" >&2
  238.     echo "Use --novendor to override or 'xdg-open --manual' for additional info." >&2
  239.     exit 1
  240. }
  241.  
  242. check_output_file()
  243. {
  244.     # if the file exists, check if it is writeable
  245.     # if it does not exists, check if we are allowed to write on the directory
  246.     if [ -e "$1" ]; then
  247.         if [ ! -w "$1" ]; then
  248.             exit_failure_file_permission_write "no permission to write to file '$1'"
  249.         fi
  250.     else
  251.         DIR=`dirname "$1"`
  252.         if [ ! -w "$DIR" -o ! -x "$DIR" ]; then
  253.             exit_failure_file_permission_write "no permission to create file '$1'"
  254.         fi
  255.     fi
  256. }
  257.  
  258. #----------------------------------------
  259. # Checks for shared commands, e.g. --help
  260.  
  261. check_common_commands()
  262. {
  263.     while [ $# -gt 0 ] ; do
  264.         parm="$1"
  265.         shift
  266.  
  267.         case "$parm" in
  268.             --help)
  269.             usage
  270.             echo "Use 'man xdg-open' or 'xdg-open --manual' for additional info."
  271.             exit_success
  272.             ;;
  273.  
  274.             --manual)
  275.             manualpage
  276.             exit_success
  277.             ;;
  278.  
  279.             --version)
  280.             echo "xdg-open 1.0.2"
  281.             exit_success
  282.             ;;
  283.         esac
  284.     done
  285. }
  286.  
  287. check_common_commands "$@"
  288.  
  289. [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL;
  290. if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then
  291.     # Be silent
  292.     xdg_redirect_output=" > /dev/null 2> /dev/null"
  293. else
  294.     # All output to stderr
  295.     xdg_redirect_output=" >&2"
  296. fi
  297.  
  298. #--------------------------------------
  299. # Checks for known desktop environments
  300. # set variable DE to the desktop environments name, lowercase
  301.  
  302. detectDE()
  303. {
  304.     if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
  305.     elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
  306.     elif `dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager > /dev/null 2>&1` ; then DE=gnome;
  307.     elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
  308.     fi
  309. }
  310.  
  311. #----------------------------------------------------------------------------
  312. # kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4
  313. # It also always returns 1 in KDE 3.4 and earlier
  314. # Simply return 0 in such case
  315.  
  316. kfmclient_fix_exit_code()
  317. {
  318.     version=`kde${KDE_SESSION_VERSION}-config --version 2>/dev/null | grep KDE`
  319.     major=`echo $version | sed 's/KDE: \([0-9]\).*/\1/'`
  320.     minor=`echo $version | sed 's/KDE: [0-9]*\.\([0-9]\).*/\1/'`
  321.     release=`echo $version | sed 's/KDE: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'`
  322.     test "$major" -gt 3 && return $1
  323.     test "$minor" -gt 5 && return $1
  324.     test "$release" -gt 4 && return $1
  325.     return 0
  326. }
  327.  
  328. # This handles backslashes but not quote marks.
  329. first_word()
  330. {
  331.     read first rest
  332.     echo "$first"
  333. }
  334.  
  335. open_kde()
  336. {
  337.     if kde-open -v 2>/dev/null 1>&2; then
  338.         kde-open "$1"
  339.     else
  340.         if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
  341.             kfmclient openURL "$1"
  342.         else
  343.             kfmclient exec "$1"
  344.             kfmclient_fix_exit_code $?
  345.         fi
  346.     fi
  347.  
  348.     if [ $? -eq 0 ]; then
  349.         exit_success
  350.     else
  351.         exit_failure_operation_failed
  352.     fi
  353. }
  354.  
  355. open_gnome()
  356. {
  357.     if gvfs-open --help 2>/dev/null 1>&2; then
  358.         gvfs-open "$1"
  359.     else
  360.         gnome-open "$1"
  361.     fi
  362.  
  363.     if [ $? -eq 0 ]; then
  364.         exit_success
  365.     else
  366.         exit_failure_operation_failed
  367.     fi
  368. }
  369.  
  370. open_xfce()
  371. {
  372.     exo-open "$1"
  373.  
  374.     if [ $? -eq 0 ]; then
  375.         exit_success
  376.     else
  377.         exit_failure_operation_failed
  378.     fi
  379. }
  380.  
  381. open_generic_xdg_mime()
  382. {
  383.     filetype=`xdg-mime query filetype "$1" | sed "s/;.*//"`
  384.     default=`xdg-mime query default "$filetype"`
  385.     if [ -n "$default" ] ; then
  386.         xdg_user_dir="$XDG_DATA_HOME"
  387.         [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
  388.  
  389.         xdg_system_dirs="$XDG_DATA_DIRS"
  390.         [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
  391.  
  392.         for x in `echo "$xdg_user_dir:$xdg_system_dirs" | sed 's/:/ /g'`; do
  393.             file="$x/applications/$default"
  394.             if [ -r "$file" ] ; then
  395.                 command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_word`"
  396.                 command_exec=`which $command 2>/dev/null`
  397.                 if [ -x "$command_exec" ] ; then
  398.                     $command_exec $1
  399.                     if [ $? -eq 0 ]; then
  400.                         exit_success
  401.                     fi
  402.                 fi
  403.             fi
  404.         done
  405.     fi
  406. }
  407.  
  408. open_generic()
  409. {
  410.     # Paths or file:// URLs
  411.     if (echo "$1" | grep -q '^file://' ||
  412.         ! echo "$1" | egrep -q '^[a-zA-Z+\.\-]+:'); then
  413.  
  414.         local file="$1"
  415.  
  416.         # Decode URLs
  417.         if echo "$file" | grep -q '^file:///'; then
  418.             file=${file#file://}
  419.             file=$(echo "$file" | perl -pe 's/%(..)/pack("c", hex($1))/eg')
  420.         fi
  421.  
  422.         check_input_file "$file"
  423.  
  424.         open_generic_xdg_mime "$file"
  425.  
  426.         if [ -f /etc/debian_version ] &&
  427.             which run-mailcap 2>/dev/null 1>&2; then
  428.             run-mailcap --action=view "$file"
  429.             if [ $? -eq 0 ]; then
  430.                 exit_success
  431.             fi
  432.         fi
  433.  
  434.         if mimeopen -v 2>/dev/null 1>&2; then
  435.             mimeopen -n "$file"
  436.             if [ $? -eq 0 ]; then
  437.                 exit_success
  438.             fi
  439.         fi
  440.     fi
  441.  
  442.     sensible-browser "$1"
  443.     if [ $? -eq 0 ]; then
  444.         exit_success
  445.     fi
  446.  
  447.     exit_failure_operation_impossible "no method available for opening '$1'"
  448. }
  449.  
  450. [ x"$1" != x"" ] || exit_failure_syntax
  451.  
  452. url=
  453. while [ $# -gt 0 ] ; do
  454.     parm="$1"
  455.     shift
  456.  
  457.     case "$parm" in
  458.       -*)
  459.         exit_failure_syntax "unexpected option '$parm'"
  460.         ;;
  461.  
  462.       *)
  463.         if [ -n "$url" ] ; then
  464.             exit_failure_syntax "unexpected argument '$parm'"
  465.         fi
  466.         url="$parm"
  467.         ;;
  468.     esac
  469. done
  470.  
  471. if [ -z "${url}" ] ; then
  472.     exit_failure_syntax "file or URL argument missing"
  473. fi
  474.  
  475. detectDE
  476.  
  477. if [ x"$DE" = x"" ]; then
  478.     DE=generic
  479. fi
  480.  
  481. case "$DE" in
  482.     kde)
  483.     open_kde "$url"
  484.     ;;
  485.  
  486.     gnome)
  487.     open_gnome "$url"
  488.     ;;
  489.  
  490.     xfce)
  491.     open_xfce "$url"
  492.     ;;
  493.  
  494.     generic)
  495.     open_generic "$url"
  496.     ;;
  497.  
  498.     *)
  499.     exit_failure_operation_impossible "no method available for opening '$url'"
  500.     ;;
  501. esac
  502.